#设定鸿蒙sdk路径
$OHOS_SDK_PATH = "D:/Program Files/Huawei/DevEco Studio/sdk"
$ARCH = "arm64-v8a" #架构 arm64-v8a

#打印PScriptRoot,不知道为什么PScriptRoot路径为空,所以这里手动设置
$PScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
$PSScriptRoot = $PScriptRoot -replace '\\', '/' #将路径中的\替换为/
Write-Host "PSScriptRoot: $PSScriptRoot"

$CMakeExe = "$OHOS_SDK_PATH/default/openharmony/native/build-tools/cmake/bin/cmake.exe"

$BuildType = "Debug" #编译类型
$BuildPath = "$PSScriptRoot/Intermediate/$BuildType"
$LibBuildPath = "$PSScriptRoot/build/$BuildType"

$Params = @(
    #输出路径
    "-B$BuildPath"
    "-DOHOS_ARCH=$ARCH"
    ,"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=$LibBuildPath"
   ,"-DCMAKE_BUILD_TYPE=$BuildType"
   ,"-DOHOS_SDK_NATIVE=$OHOS_SDK_PATH/default/openharmony/native"
   ,"-DCMAKE_SYSTEM_NAME=OHOS"
   ,"-DCMAKE_OHOS_ARCH_ABI=arm64-v8a"
   #生成编译命令json 文件,可以观察编译过程
   ,"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
    ,"-DCMAKE_TOOLCHAIN_FILE=$OHOS_SDK_PATH/default/hms/native/build/cmake/hmos.toolchain.cmake"
    ,"-GNinja"
    ,"-DCMAKE_MAKE_PROGRAM=$OHOS_SDK_PATH/default/openharmony/native/build-tools/cmake/bin/ninja.exe"
    ,"-DHMOS_SDK_NATIVE=$OHOS_SDK_PATH/default/hms/native"
    #此外还有DCMAKE_FIND_ROOT_PATH 和 PACKAGE_FIND_FILE 等参数可以设置
    #由于鸿蒙提供的 PACKAGE_FIND_FILE 文件为空,所以这里没有设置 
)

#执行配置命令
& $CMakeExe  $Params

#执行编译命令
& $CMakeExe --build $BuildPath --config $BuildType





##执行Release编译

$BuildType = "Release" #编译类型
$BuildPath = "$PSScriptRoot/Intermediate/$BuildType"
$LibBuildPath = "$PSScriptRoot/build/$BuildType"

$Params = @(
    #输出路径
    "-B$BuildPath"
    "-DOHOS_ARCH=$ARCH"
    ,"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=$LibBuildPath"
   ,"-DCMAKE_BUILD_TYPE=$BuildType"
   ,"-DOHOS_SDK_NATIVE=$OHOS_SDK_PATH/default/openharmony/native"
   ,"-DCMAKE_SYSTEM_NAME=OHOS"
   ,"-DCMAKE_OHOS_ARCH_ABI=arm64-v8a"
   #生成编译命令json 文件,可以观察编译过程
   ,"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
    ,"-DCMAKE_TOOLCHAIN_FILE=$OHOS_SDK_PATH/default/hms/native/build/cmake/hmos.toolchain.cmake"
    ,"-GNinja"
    ,"-DCMAKE_MAKE_PROGRAM=$OHOS_SDK_PATH/default/openharmony/native/build-tools/cmake/bin/ninja.exe"
    ,"-DHMOS_SDK_NATIVE=$OHOS_SDK_PATH/default/hms/native"
    #此外还有DCMAKE_FIND_ROOT_PATH 和 PACKAGE_FIND_FILE 等参数可以设置
    #由于鸿蒙提供的 PACKAGE_FIND_FILE 文件为空,所以这里没有设置 
)

#执行配置命令
& $CMakeExe  $Params

#执行编译命令
& $CMakeExe --build $BuildPath --config $BuildType

#把所有的库放到指定文件夹下，方便复制
$LibPath = "$PSScriptRoot/ThirdParty"
$PngPathDebug = "$PSScriptRoot/ThirdParty/libpng/OHOS_$ARCH/Debug"
$PngPathRelease = "$PSScriptRoot/ThirdParty/libpng/OHOS_$ARCH/Release"
$PngPathInclude = "$PSScriptRoot/ThirdParty/libpng/OHOS_$ARCH/include"


$JpegPathDebug = "$PSScriptRoot/ThirdParty/libjpeg/OHOS_$ARCH/Debug"
$JpegPathRelease = "$PSScriptRoot/ThirdParty/libjpeg/OHOS_$ARCH/Release"
$JpegPathInclude = "$PSScriptRoot/ThirdParty/libjpeg/OHOS_$ARCH/include"

$FreetypePathDebug = "$PSScriptRoot/ThirdParty/freetype/OHOS_$ARCH/Debug"
$FreetypePathRelease = "$PSScriptRoot/ThirdParty/freetype/OHOS_$ARCH/Release"
$FreetypePathInclude = "$PSScriptRoot/ThirdParty/freetype/OHOS_$ARCH/include"

$AllDirs = @(
    $PngPathDebug,
    $PngPathRelease,
    $PngPathInclude,
    $JpegPathDebug,
    $JpegPathRelease,
    $JpegPathInclude,
    $FreetypePathDebug,
    $FreetypePathRelease,
    $FreetypePathInclude 
)
#创建目录
foreach ($Dir in $AllDirs) {
    if (-not (Test-Path -Path $Dir -PathType Container)) {
        New-Item -ItemType Directory -Path $Dir -Force | Out-Null
    } 
}

#复制libpng
Copy-Item -Path "$PSScriptRoot/build/Debug/libpng16d.so" -Destination "$PngPathDebug" -Force
Copy-Item -Path "$PSScriptRoot/build/Release/libpng16.so" -Destination "$PngPathRelease" -Force

#复制头文件
$PngHeaders = @(
    "$PSScriptRoot/Libs/libpng/png.h"
    "$PSScriptRoot/Libs/libpng/pngconf.h"
    "$BuildPath/Libs/libpng/pnglibconf.h"
)
foreach ($PngHeader in $PngHeaders) {
    Copy-Item -Path "$PngHeader" -Destination "$PngPathInclude" -Force
}


#复制libjpeg
Copy-Item -Path "$PSScriptRoot/build/Debug/libjpegd.so" -Destination "$JpegPathDebug" -Force
Copy-Item -Path "$PSScriptRoot/build/Release/libjpeg.so" -Destination "$JpegPathRelease" -Force

#复制头文件
$JpegHeaders = @(
    "$BuildPath/Libs/libjpeg/jmorecfg.h"
    "$BuildPath/Libs/libjpeg/jpeglib.h"
    "$BuildPath/Libs/libjpeg/jerror.h"
    "$BuildPath/Libs/libjpeg/jconfig.h"
    "$PSScriptRoot/Libs/libjpeg/jversion.h"
)

foreach ($JpegHeader in $JpegHeaders) {
    Copy-Item -Path "$JpegHeader" -Destination "$JpegPathInclude" -Force 
}

#复制freetype
Copy-Item -Path "$PSScriptRoot/build/Debug/libfreetyped.so" -Destination "$FreetypePathDebug" -Force
Copy-Item -Path "$PSScriptRoot/build/Release/libfreetype.so" -Destination "$FreetypePathRelease" -Force

#复制头文件
$FreetypeHeaders = @(
    "$PSScriptRoot/Libs/freetype/include"
)

foreach ($FreetypeHeader in $FreetypeHeaders) {
    Copy-Item -Path "$FreetypeHeader" -Destination "$FreetypePathInclude" -Force -Recurse 
}